package com.taru.controller;


import com.github.pagehelper.PageHelper;
import com.taru.model.Fruits;
import com.taru.service.FruitService;
import com.taru.utils.Constant;
import com.taru.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
public class FruitController {
    private static Logger logger = LoggerFactory.getLogger(FruitController.class);

    @Autowired
    FruitService fruitService;


    @RequestMapping("/query/fruits/id")
    @ResponseBody
    public Object queryFruitsById(String fiuitsId){
            JsonResult result;
        try {
            Fruits fruits= fruitService.queryFruitsById(fiuitsId);
            logger.info("id{}", fiuitsId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, fruits);
            logger.info("查询结果", fruits.toString());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/insert/fruits")
    @ResponseBody
    public Object insertFruits(Fruits fruits){
        JsonResult result = null;
        try {
            int i = fruitService.insertFruits(fruits);
            if(i == 0){
                logger.info("添加", fruits.toString());
                result = new JsonResult("添加失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", fruits.toString());
                result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
            }

        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }


    @RequestMapping("/update/fruits/id")
    @ResponseBody
    public Object updateFruitsById(Fruits fruits){
        JsonResult result = null;
        try {
           int i = fruitService.updateFruitsById(fruits);
            if(i == 0){
                logger.info("修改", fruits.toString());
                result = new JsonResult("修改失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", fruits.toString());
                result = new JsonResult("修改成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("修改异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/delete/fruits/id")
    @ResponseBody
    public Object deleteFruitsById(String fiuitsId){
        JsonResult result = null;
        try {
           int i = fruitService.deleteFruitsById(fiuitsId);
            if(i == 0){
                logger.info("删除", fiuitsId);
                result = new JsonResult("删除失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("删除", fiuitsId);
                result = new JsonResult("删除成功", Constant.STATUC_SUCCESS);
            }

        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

}
